package com.bdh.demo.framework;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.CollectionUtils;

import java.util.Properties;

/**
 * Created by baodehua on 2016/11/25.
 *
 * PropertyPlaceholderConfigurer用于Spring从外部属性文件中载入属性，并使用这些属性值替换Spring配置文件中的占位符变量（${varible}）。
 * BeanFactoryPostProcessor接口是对Bean工厂的后处理操作，Spring的PropertyPlaceholderConfigurer类是实现BeanFactoryProcessor接口中非常有用的类。
 * 继承PropertyPlaceholderConfigurer类可以实现对配置的自定义处理。
 */
public class DemoPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        Properties configurationProperties = new Properties();
        configurationProperties.put("demo.db.master.driver", "");
        configurationProperties.put("demo.db.master.url", "");
        configurationProperties.put("demo.db.master.user", "");
        configurationProperties.put("demo.db.master.password", "");
        CollectionUtils.mergePropertiesIntoMap(props, configurationProperties);
        super.processProperties(beanFactoryToProcess, configurationProperties);
    }

}
